package hikari.managers;

import com.gooddies.wiring.annotations.WiringComponent;
import hikari.objects3d.Basic3dObject;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA. User: Dmitry Date: 24.11.12 Time: 21:29
 */
@WiringComponent(singleton = true)
public class SelectionManager {

    private Basic3dObject previousSelectedObject = null;
    private ArrayList<SelectionEvent> events = new ArrayList<>(10);
    private boolean disableSelection = false;

    private SelectionManager() {
    }

    public void clearSelectedObject() {
        previousSelectedObject = null;
    }

    public Basic3dObject getCurrentSelectedObject() {
        return previousSelectedObject;
    }

    public void addSelectionEvent(SelectionEvent event) {
        events.add(event);
    }

    public void removeSelectionEvent(SelectionEvent event) {
        events.remove(event);
    }

    public void disableChangeSelection() {
        disableSelection = true;
    }

    public void enableChangeSelection() {
        disableSelection = false;
    }

    public boolean isSelectionDisabled() {
        return disableSelection;
    }

    public boolean select(Basic3dObject object) {
        if (isSelectionDisabled()) {
            return false;
        }

        for (SelectionEvent event : events) {
            if (!event.isCanSelect(getCurrentSelectedObject(), object)) {
                return false;
            }
        }
        for (SelectionEvent event : events) {
            event.onSelected(getCurrentSelectedObject(), object);
        }

        previousSelectedObject = object;
        return true;
    }

    public static interface SelectionEvent {

        public void onSelected(Basic3dObject currentSelection, Basic3dObject newSelection);

        public boolean isCanSelect(Basic3dObject currentSelection, Basic3dObject newSelection);
    }
}
